package com.mt.common.core.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: RabbitMqConfig
 * @Description:
 * @Author zhanghong
 * @Date 2022/5/16
 */
@Configuration
public class RabbitMqConfig {
    /**
     * 死信队列
     */
    public static final String DLX_QUEUE = "dlx_queue";
    public static final String DLX_EXCHANGE = "dlx_exchange";
    public static final String DLX_ROUTING_KEY = "dlx_routing_key";

    /**
     * 正常队列
     */
    public static final String MSG_QUEUE = "msg_queue";
    public static final String MSG_EXCHANGE = "msg_exchange";
    public static final String MSG_ROUTING_KEY = "msg_routing_key";

    /**
     * 死信队列
     *
     * @return
     */
    @Bean
    Queue dlxQueue() {
        return new Queue(DLX_QUEUE, true, false, false);
    }

    /**
     * 死信交换机
     *
     * @return
     */
    @Bean
    DirectExchange dlxExchange() {
        return new DirectExchange(DLX_EXCHANGE, true, false);
    }

    /**
     * 绑定死信队列和死信交换机
     *
     * @return
     */
    @Bean
    Binding dlxBinding() {
        return BindingBuilder.bind(dlxQueue()).to(dlxExchange())
                .with(DLX_ROUTING_KEY);
    }

    /**
     * 普通消息队列
     *
     * @return
     */
    @Bean
    Queue msgQueue() {
        Map<String, Object> args = new HashMap<>();
        //设置消息过期时间
        args.put("x-message-ttl", 1000 * 60 * 5);
        //设置死信交换机
        args.put("x-dead-letter-exchange", DLX_EXCHANGE);
        //设置死信 routing_key
        args.put("x-dead-letter-routing-key", DLX_ROUTING_KEY);
        return new Queue(MSG_QUEUE, true, false, false, args);
    }

    /**
     * 普通交换机
     *
     * @return
     */
    @Bean
    DirectExchange msgExchange() {
        return new DirectExchange(MSG_EXCHANGE, true, false);
    }

    /**
     * 绑定普通队列和与之对应的交换机
     *
     * @return
     */
    @Bean
    Binding msgBinding() {
        return BindingBuilder.bind(msgQueue())
                .to(msgExchange())
                .with(MSG_ROUTING_KEY);
    }


    //@Bean("listenerContainerFactory")
    //public SimpleRabbitListenerContainerFactory listenerContainerFactory(ConnectionFactory connectionFactory) {
    //    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    //    factory.setConnectionFactory(connectionFactory);
    //    // 如果需要批量确认消息，则做以下设置
    //    // 设置批量
    //    // factory.setBatchListener(true);
    //    // 设置BatchMessageListener生效
    //    // factory.setConsumerBatchEnabled(true);
    //    // 设置批量确认数量
    //    // factory.setBatchSize(10);
    //
    //    //设置消息为手动确认
    //    factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
    //
    //    return factory;
    //}
}
